import * as constants from './constants'
import api from '../../../api/api'
import { message } from 'antd';
import Utils from '../../../common/utils'
import Immutable from 'immutable';
import { getLesson } from '../../../page/home/store/actionCreators'
import { getCourseDescript } from '../../../page/home/store/actionCreators'
import { getCourseList } from '../../../page/course/store/actionCreators'

export const changeLogin = (result) => ({
	type: constants.CHANGE_LOGIN,
	value: Immutable.fromJS(result)
})

const changeSms = (result) => ({
  type: constants.CHANGE_SMS,
  value: Immutable.fromJS(result)
})

const changeSmsCount = (result) => ({
  type: constants.CHANGE_SMSCOUNT,
  value: Immutable.fromJS(result)
})

const changeCartList = (result) => ({
  type: constants.CHANGE_CARTLIST,
  value: Immutable.fromJS(result)
})

const changeSearch = (result) => ({
  type: constants.CHANGE_SEARCH,
  value: Immutable.fromJS(result)
})

export const changeShowType = (result) => ({
  type: constants.CHANGE_SHOWTYPE,
  value: result
})

export const changeShowLogin = (result) => ({
  type: constants.CHANGE_SHOWLOGIN,
  value: result
})

export const ClearUser = () => (dispatch)=> (
  api.logout().then(res => {
    if(res.code === 20000) {
      dispatch(changeLogin(''))
      dispatch(getLesson({eduRank: '小学', type: 'all'}))
      dispatch(getLesson({eduRank: '初中', type: 'all'}))
      dispatch(getLesson({eduRank: '高中', type: 'all'}))
    } else {
      message.error(res.msg);
    }
  })
)

export const ClearPhoneUser = () => (dispatch)=> (
  api.logout().then(res => {
    if(res.code === 20000) {
      dispatch(changeLogin(''))
      localStorage.clear()
      window.location.href="/logout"
    } else {
      message.error(res.msg);
    }
  })
)

export const sendSms = (params) => (dispatch) => {
  api.sendSms(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeSms(res.data))
      let s = 120
      let timer = setInterval(() => {
        s = s-1
        dispatch(changeSmsCount(s))
        if(s === 0) {
          clearInterval(timer)
        }
      }, 1000);

    }
  })
}

export const singUp = (params) => (dispatch) => {
  api.signUp(params).then(res => {
    if(res.code === 20000) {
      message.success('注册成功！')
      setTimeout(()=> {
        dispatch(changeShowType('login'))
      }, 2000)
    }
  })
}

export const getCartList = () => (dispatch) => {
  api.getCartList().then(res => {
    if(res.code === 20000) {
      dispatch(changeCartList(res.data))
    }
  })
}


export const Login = (params, str, search)=> {
  return (dispatch)=> {
    api.login(params).then(res => {
      if (res.code === 20000) {
        Utils.local('userData', res.data)
        Utils.session('user', res.data)
        dispatch(getCartList())
        dispatch(changeLogin(res.data))
        // dispatch(getLesson({eduRank: '小学', type: 'all'}))
        // dispatch(getLesson({eduRank: '初中', type: 'all'}))
        // dispatch(getLesson({eduRank: '高中', type: 'all'}))
        let check = str && str.indexOf('courseitem')!=-1 ? true: false
        if(check) {
          dispatch(getCourseDescript({
            lessonId: str.split('/')[2]
          }))
        }
        debugger
        let course = str && str.indexOf('course')!=-1 ? true: false
        if(course) {
          dispatch(getCourseList({
            grade: '全部',
            subjectName: '全部',
            type: 'all',
            key: search,
            page: 1,
            lessonType: '',
            isOrderBySubject: false,
            isOrderByYear: false
          }))
        }
      } else {
        message.error(res.msg);
      }
    })
  }
}

export const search = (str) => (dispatch) => {
  dispatch(changeSearch(str))
} 

export const findPassword = (params) => (dispatch) => {
  api.findPassword(params).then(res => {
    if(res.code === 20000) {
      message.success('密码设置成功！')
      setTimeout(()=> {
        dispatch(changeShowType('login'))
      }, 2000)
    }
  })
}

export const checkLogin = (params) => (dispatch) => {
  api.checkLogin().then(res=> {
    if(res.code === 20000) {
      Utils.local('userData', res.data)
      dispatch(changeLogin(res.data))
    }
  })
}